/*公共js文件*/

$(document).ready( function () {
	$('.js-ajax-submit').click(function () {
		var btn = $(this);
		var btn_text = btn.text();
		
		if (btn.is('.login')) {
			if (!checkLogin()) return false;
		}
		
		btn.prop("disabled", true);
		btn.text(btn_text+'中...');
		$('.js-ajax-form').ajaxSubmit({
			success:function (data) {
				var msg = data.msg;
				if (data.code == 1) {
                    if (btn.is('.login')) {
                        layer.msg(msg, {icon:1, time:3000}, function(){
                            if (data.url != '') {
                                window.location.href=data.url;
                            } else {
                                location.reload();
                            }
                        });
                    } else {
                        layer.msg(msg, {icon:1, time:3000}, function(){
                            //layer.close(close_index);
                            if (data.url != '') {
                                window.location.href=data.url;
                            } else {
                                location.reload();
                            }
                        });
                    }
				} else {
					layer.msg(msg, {icon:2, time:3000}, function(){
						if (btn.is('.login')) {
							$("#captcha").click();
							$(".captcha").val('');
						}
						btn.prop("disabled", false);
						btn.text(btn_text);
						//layer.close(close_index);
					});
				}
			},
			error:function (err) {
				layer.alert("表单提交异常！", {icon:2}, function(close_index){
					btn.prop("disabled", false);
					btn.text(btn_text);
					layer.close(close_index);
				});
			}
		});
		return false;
	});
	
	$('.js-ajax-del').click(function () {
        var $this    = $(this);
		var del_url  = $this.prop('href');
        var hint_msg = $this.data('hint-msg');
        if (!hint_msg) {
            hint_msg = '是否确认删除？';
        }
		layer.confirm(hint_msg, {icon: 3, title:'提示'}, function(close_index){
			layer.close(close_index);
			$.get(del_url, function(data) {
				var msg = data.msg;
				if (data.code == 1) {
					layer.alert(msg, {icon:1}, function(close_index){
						layer.close(close_index);
						location.reload();
					});
				} else {
					layer.alert(msg, {icon:2}, function(close_index){
						layer.close(close_index);
					});
				}
			});
		});
		return false;
	});
	
});

function checkLogin(){
	var account = $(".account").val();
		pwd		= $(".password").val();
		captcha = $(".captcha").val();
	if (account == '') {
		layer.alert('请输入登录帐号', {icon: 0});
		return false;
	}
	if (pwd == '') {
		layer.alert('请输入登录密码', {icon: 0});
		return false;
	}
	if (captcha == '') {
		layer.alert('请输入验证码', {icon: 0});
		return false;
	}
	
	return true;
}

// 获取HTML5特性支持情况
function checkSupport(){
    var input = document.createElement('input');
    var fileSupport = !!(window.File && window.FileList);
    var xhr = new XMLHttpRequest();
    var fd = !!window.FormData;
    return 'multiple' in input && fileSupport && 'onprogress' in xhr && 'upload' in xhr && fd ? 'html5' : 'flash';
};

/**
 * 移除图片功能
 * remove_Class 要移除的class
 * upload_id 上传控件的id
 **/
function remove_img(remove_Class, upload_id){
    if(remove_Class == ''){
        layer.alert('操作失败！', {
            icon: 2,
            title: '提示信息'
        });
    }
    layer.confirm('确定删除？', {icon: 3, title: '提示信息'}, function(index){
        layer.close(index);
        $('.'+remove_Class).remove();
    });
}

//日期选择器
laydate.render({ 
    elem: '#date_start',
});
laydate.render({ 
    elem: '#date_end',
});

//日期时间选择器
laydate.render({ 
    elem: '#laydate_time',
    type: 'datetime'
});
laydate.render({ 
    elem: '#start_time',
    type: 'datetime'
});
laydate.render({ 
    elem: '#end_time',
    type: 'datetime'
});